
using System;
using UnityEngine;
using UnityEngine.UI;

namespace MessageBoxNS
{
    public enum ShowMessageBoxType
    {
        Confirm,
        ConfirmAndCancle,
        OverTime
    }
    public class MessageBox
    {
        public ShowMessageBoxType MessageBoxType { get; set; }
        public string HintInfo { get; set; }
        public string ConfirmStr { get; set; }
        public string CancleStr { get; set; }
        public string PrefabPath { get; set; }
        public float HideTime { get; set; }
        public Action ConfirmAction { get; set; }
        public Action CancleAction { get; set; }
        public Action OverTimeAction { get; set; }

        public GameObject Box = null;

        public MessageBox(ShowMessageBoxType messageBoxType, string prefabPath, string hintInfo, string confirmStr, string cancleStr, Action confirmAction, Action cancleAction, Action overTimeAction, float hideTime)
        {
            this.MessageBoxType = messageBoxType;
            this.PrefabPath = prefabPath;
            this.HintInfo = hintInfo;
            this.ConfirmStr = confirmStr;
            this.CancleStr = cancleStr;
            this.ConfirmAction = confirmAction;
            this.CancleAction = cancleAction;
            this.OverTimeAction = overTimeAction;
            this.HideTime = hideTime;

            Box = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(PrefabPath), GameObject.Find("Canvas").transform);
            Box.transform.localPosition = Vector3.zero;
            Box.transform.localScale = Vector3.one;

            Button confirmBtn;
            Button cancelBtn;
            Text hintInfoTxt;
            BindindToData(Box, out confirmBtn, out cancelBtn, out hintInfoTxt);
            switch (messageBoxType)
            {
                case ShowMessageBoxType.Confirm:
                    cancelBtn.gameObject.SetActive(false);
                    confirmBtn.gameObject.SetActive(true);
                    confirmBtn.transform.localPosition = new Vector3(0, confirmBtn.transform.localPosition.y, confirmBtn.transform.localPosition.z);
                    break;
                case ShowMessageBoxType.ConfirmAndCancle:
                    confirmBtn.gameObject.SetActive(true);
                    cancelBtn.gameObject.SetActive(true);
                    break;
                case ShowMessageBoxType.OverTime:
                    confirmBtn.gameObject.SetActive(false);
                    cancelBtn.gameObject.SetActive(false);
                    Box.AddComponent<OverTimeMessageBoxCallBack>();
                    Box.GetComponent<OverTimeMessageBoxCallBack>().DestroyCallBack = overTimeAction;
                    Box.GetComponent<OverTimeMessageBoxCallBack>().timer = hideTime;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 绑定数据到实例化出的Box面板
        /// </summary>
        /// <param name="box"></param>
        private void BindindToData(GameObject box, out Button confirmBtn, out Button cancelBtn, out Text hintInfoTxt)
        {
            //获取
            confirmBtn = box.transform.Find("confrimBtn").GetComponent<Button>();
            cancelBtn = box.transform.Find("cancelBtn").GetComponent<Button>();
            hintInfoTxt = box.transform.Find("HintInfo").GetComponentInChildren<Text>();

            //赋值
            hintInfoTxt.text = HintInfo;
            confirmBtn.GetComponentInChildren<Text>().text = ConfirmStr;
            cancelBtn.GetComponentInChildren<Text>().text = CancleStr;

            confirmBtn.onClick.AddListener(() =>
            {
                if (ConfirmAction != null)
                {
                    ConfirmAction();
                }
                ClosePanel(box);
            });
            cancelBtn.onClick.AddListener(() =>
            {
                if (CancleAction != null)
                {
                    CancleAction();
                }
                ClosePanel(box);
            });
        }

        /// <summary>
        /// 清除面板
        /// </summary>
        /// <param name="timer"></param>
        private void ClosePanel(GameObject box, float timer = 0)
        {
            UnityEngine.Object.Destroy(box, timer);
        }

        /// <summary>
        /// 实例化通用提示框(等待HideTime之后,销毁提示框)
        /// </summary>
        public static void ShowOverTimeMessageBox(string prefabName, string hintInfo, float hideTime = 2, Action overTimeAction = null)
        {
            MessageBox boxInfo = new MessageBox(ShowMessageBoxType.OverTime, "MessageBox/" + prefabName, hintInfo, null, null, null, null, overTimeAction, hideTime);
        }
        /// <summary>
        /// 实例化通用提示框(单按钮确定,销毁提示框)
        /// </summary>
        public static void ShowConfirmMessageBox(string prefabName, string hintInfo, string confirmStr, Action comfirmAction = null)
        {
            MessageBox boxInfo = new MessageBox(ShowMessageBoxType.Confirm, "MessageBox/" + prefabName, hintInfo, confirmStr, "", comfirmAction, null, null, 1);
        }
        /// <summary>
        /// 实例化通用提示框(双按钮确定,销毁提示框)
        /// </summary>
        public static void ShowConfirmAndCancleMessageBox(string prefabName, string hintInfo, string confirmStr, string cancleStr, Action comfirmAction = null, Action cancleAction = null)
        {
            MessageBox boxInfo = new MessageBox(ShowMessageBoxType.ConfirmAndCancle, "MessageBox/" + prefabName, hintInfo, confirmStr, cancleStr, comfirmAction, cancleAction, null, 1);
        }
    }
}

